package com.lx.util;

import com.lx.entity.Book;

import java.time.LocalDate;
import java.util.*;

/**
 * @author c先生
 * @date 2022年04月12日 上午 11:55:25
 * @describe  操作书籍的工具类
 * 书籍管理系统
 * 1.注册功能(使用对象的序列化和反序列化实现)
 * 2.登录功能(使用对象的反序列化实现)
 * 3.展示书籍信息(按照书籍类型进行分类展示)
 *   要求：使用HashMap存储书籍信息；key(String) 存储书籍的类型，value(ArrayList<Book>)；存储书籍的集合
 *             Book属性：书籍编号，书籍名称，书籍作者，书籍价格，书籍简介，书籍出版时间，书籍出版社
 * 4.根据出版社名称查询旗下所有的出版书籍信息
 * 5.根据书籍作者查询旗下的所有书籍信息
 * 6.根据书籍名称模糊查询对应的书籍信息(输入  三  展示  三国演义  三国志)
 * 7.新增一本书籍信息(如果书籍对应的类型存在则存储在对应的value中，如果不存在则新建一组key  value进行存储)
 * 8.根据书籍名称修改书籍信息(要求：修改前展示书籍对应信息)
 * 9.根据书籍名称删除对应书籍信息
 */

public class BookUtil {
    HashMap<String, List> bookMap = new HashMap<>();

    public BookUtil() {
        init();
    }

    //根据书籍名称删除对应书籍信息
    public boolean deleteBook(String bookName) {
        Set<String> keySet = bookMap.keySet();
        for (String bookKey : keySet) {
            List<Book> bookList = bookMap.get(bookKey);
            for (Book book : bookList) {
                if (book.getBookName().equals(bookName)) {
                    bookList.remove(book);
                    return true;
                }
            }
        }
        return false;
    }
    //查询书籍是否存在
    public Book findBookByName(String bookName){
        Set<String> keySet = bookMap.keySet();
        for (String bookKey : keySet) {
            List<Book> bookList = bookMap.get(bookKey);
            for (Book book : bookList) {
                if(book.getBookName().equals(bookName)){
                    return book;
                }
            }
        }
        return null;
    }
    //新增一本书籍信息
    public boolean addBook(String bookType,Book book){
        Set<String> keySet = bookMap.keySet();
        boolean flag = false;
        for (String bookKey : keySet) {
            if(bookKey.equals(bookType)){
                List<Book> bookList = bookMap.get(bookKey);
                bookList.add(book);
                bookMap.put(bookType,bookList);
                flag = true;
                break;
            }
        }
        if(flag == false) {
            List<Book> bookList2 = new ArrayList<>();
            bookList2.add(book);
            bookMap.put(bookType, bookList2);
            flag = true;
        }
        return flag;
    }

    //根据书籍名称模糊查询对应的书籍信息
    public void findBookInfo(String bookName) {
        Set<String> keySet = bookMap.keySet();
        for (String bookKey : keySet) {
            List<Book> bookList = bookMap.get(bookKey);
            for (Book book : bookList) {
                if (book.getBookName().contains(bookName)) {
                    System.out.println(book.toString());
                }
            }
        }
    }

    //根据书籍作者查询旗下的所有书籍信息
    public void findBookByAuthor(String Author) {
        Set<String> keySet = bookMap.keySet();
        for (String bookKey : keySet) {
            List<Book> bookList = bookMap.get(bookKey);
            for (Book book : bookList) {
                if (book.getBookAuthor().equals(Author)) {
                    System.out.println(book.toString());
                }
            }
        }
    }

    //根据出版社名称查询旗下所有的出版书籍信息
    public void findBookByPublisher(String publisherName) {
        Set<String> keySet = bookMap.keySet();
        for (String bookKey : keySet) {
            List<Book> bookList = bookMap.get(bookKey);
            for (Book book : bookList) {
                if (book.getBookPublisher().equals(publisherName)) {
                    System.out.println(book.toString());
                }
            }
        }
    }
    //查询所有的出版社
    public HashSet<String> findAllPublisher(){
        Set<String> keySet = bookMap.keySet();
        HashSet<String> hashSet = new HashSet<>();
        for (String bookKey : keySet) {
            List<Book> bookList = bookMap.get(bookKey);
            for (Book book : bookList) {
                hashSet.add(book.getBookPublisher());
            }
        }
        return hashSet;
    }
    //展示所有书籍信息
    public void showAllBook(){
        Set<String> keySet = bookMap.keySet();
        for (String bookKey : keySet) {
            System.out.println(bookKey+"书籍如下：");
            List<Book> bookList = bookMap.get(bookKey);
            for (Book book : bookList) {
                System.out.println(book.toString());
            }
        }
    }

    //初始化书籍信息
    public void init(){
        String bookType1 = "故事类";
        List<Book> bookList1 = new ArrayList<>();
        LocalDate time1 = LocalDate.of(2001,1,1);
        Book book1 = new Book(1001,"狼王梦","沈石溪",38.8,
                "这是一本讲述狼王的故事书",time1,"新华出版社");
        LocalDate time2 = LocalDate.of(2002,2,2);
        Book book2 = new Book(1002,"残狼灰满","沈石溪",18.8,
                "这又双叒叕是一本讲述狼王的故事书",time2,"新华出版社");
        LocalDate time3 = LocalDate.of(2003,3,3);
        Book book3 = new Book(1003,"斑羚飞渡","沈石溪",8.8,
                "这是一本讲述狼王的食物如何逃生的故事书",time3,"清华出版社");
        bookList1.add(book1);
        bookList1.add(book2);
        bookList1.add(book3);
        bookMap.put(bookType1,bookList1);

        String bookType2 = "科幻类";
        List<Book> bookList2 = new ArrayList<>();
        LocalDate time4 = LocalDate.of(2004,4,4);
        Book book4 = new Book(1004,"外星人是如何炼成的","张恒",98.8,
                "这是一本讲述作者如何把自己变成外星人的科幻书",time4,"清华出版社");
        LocalDate time5 = LocalDate.of(2005,5,5);
        Book book5 = new Book(1004,"三体","刘慈欣",198.8,
                "这是一本讲述外星人侵略地球的科幻书",time5,"清华出版社");
        LocalDate time6 = LocalDate.of(2006,6,6);
        Book book6 = new Book(1006,"流浪张恒","李欣",58.8,
                "这是一本讲述作者如何把罪人张恒流放到外星球的科幻书",time6,"新华出版社");
        bookList2.add(book4);
        bookList2.add(book5);
        bookList2.add(book6);
        bookMap.put(bookType2,bookList2);
    }
}
